﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.Xml;

namespace OpenBot
{
    public partial class OpenBot : Form
    {

        string filePathToBot;
        int maxBots;
        int IPsPerBot = 14;
        List<string> listOfIPs = new List<string>();

        public OpenBot()
        {
            InitializeComponent();

            //Set max on bot number (ceil(number of IP's / IPsPerBot))
            string linkToListOfIPs = "http://www.squiblo.co.uk/AdflyBot/listOfIPs.xml";

            //Get list of IP's
            XmlDocument XMLlistOfIPs = new XmlDocument();
            XMLlistOfIPs.Load(linkToListOfIPs);

            //Populate listOfIPs
            foreach (XmlNode IP in XMLlistOfIPs.DocumentElement.ChildNodes)
            {
                listOfIPs.Add(IP.InnerText);
            }

            //Set max for bot number
            decimal IPsPerBot_Decimals = Convert.ToDecimal(listOfIPs.Count()) / IPsPerBot;
            maxBots = Convert.ToInt32(Math.Ceiling(IPsPerBot_Decimals));
            lblMaxBots.Text = "(Max = " + maxBots + ")";
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnBrowse_Click(object sender, EventArgs e)
        {
            openBotDialog.FileName = string.Empty;
            openBotDialog.Filter = "Executable files (*.exe)|*.exe";

            if (openBotDialog.ShowDialog() == DialogResult.OK)
            {
                filePathToBot = openBotDialog.FileName;
                txtBotPath.Text = filePathToBot;
            }
        }

        private void txtOpenTimes_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar))
            {
                e.Handled = true;
            }
        }

        private void btnOpen_Click(object sender, EventArgs e)
        {

            lblError.Text = "";

            if (txtBotPath.Text != "")
            {
                if (txtOpenTimes.Text != "")
                {

                    int openTimes = Convert.ToInt32(txtOpenTimes.Text);

                    if (openTimes > 0)
                    {
                        if (openTimes <= maxBots)
                        {

                            for (int i = 0; i < openTimes; i++)
                            {
                                Process process = new Process();
                                process.StartInfo.FileName = filePathToBot;
                                process.Start();
                            }

                            this.Close();
                        }
                        else
                        {
                            lblError.Text = "Exceeds number of max bots";
                        }
                    }
                    else
                    {
                        lblError.Text = "Cannot open 0 times";
                    }                    
                }
                else
                {
                    lblError.Text = "Enter number of times";
                }
            }
            else
            {
                lblError.Text = "No file selected";
            }
        }

    }
}
